package com.bigmodel.baidu;

import cn.hutool.core.text.UnicodeUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class WizardcoderClient {
    private String  cahtUrl;

    public static void main(String[] args) {
        String str = "\"import java.time.*;\\r\\nimport java.time.format.*;\\r\\nimport java.time.temporal.*;\\r\\n\\r\\npublic class GPTDateTime {\\r\\n    public String[] getStartEndTime() {\\r\\n        \\/\\/\\u83b7\\u53d6\\u5f53\\u524d\\u65f6\\u95f4\\r\\n        LocalDateTime now = LocalDateTime.now();\\r\\n        \\/\\/\\u6b65\\u9aa4\\u4e00:\\u5f00\\u59cb\\u65f6\\u95f4\\u8ba1\\u7b97\\r\\n        LocalDateTime startTime = now.toLocalDate().atStartOfDay();\\r\\n        \\/\\/\\u6b65\\u9aa4\\u4e8c:\\u7ed3\\u675f\\u65f6\\u95f4\\u8ba1\\u7b97\\r\\n        LocalDateTime endTime = now.toLocalDate().atTime(23, 59, 59);\\r\\n        \\/\\/\\u6b65\\u9aa4\\u4e09:\\u683c\\u5f0f\\u5316\\u5f00\\u59cb\\u65f6\\u95f4,\\u7ed3\\u675f\\u65f6\\u95f4\\r\\n        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(\\\"yyyy-MM-dd HH:mm:ss\\\");\\r\\n        String startTimeStr = formatter.format(startTime);\\r\\n        String endTimeStr = formatter.format(endTime);\\r\\n        \\/\\/\\u6b65\\u9aa4\\u56db:\\u8fd4\\u56de\\u6570\\u7ec4\\r\\n        return new String[]{startTimeStr, endTimeStr};\\r\\n    }\\r\\n}\"\n";


        str = UnicodeUtil.toString(str);
        str = str.replaceAll("\\\\r\\\\n","\r\n");
        str = str.replaceAll("\\\\/\\\\/","//");
        str = str.replaceAll("\\\\\"","\"");
        str = str.substring(1, str.lastIndexOf("}")+1);

        System.out.println(str);

        //chat(null);

    }


    public  static String chat(List<String> prompt){


//      String url = "http://139.196.7.196:21103/chat2db/wizardcoder";
        String url = "http://120.78.182.15:21103/wizardcoder";

        Map<String,Object> params = new HashMap<>();
        params.put("ask", prompt.get(0));

        HttpRequest reuqest = HttpUtil.createPost(url);
        Map<String,String> headers = new HashMap<>();
        headers.put("Content-Type","application/json;charset=utf-8");
        reuqest.addHeaders(headers);
        reuqest.body(JSONUtil.toJsonStr(params));


        String str = reuqest.execute().body();

        str = UnicodeUtil.toString(str);
        str = str.replaceAll("\\\\r\\\\n","\r\n");
        str = str.replaceAll("\\\\/\\\\/","//");
        str = str.replaceAll("\\\\\"","\"");
        str = str.substring(1, str.lastIndexOf("}")+1);

//        System.out.println(str);

        return str;
    }

/*    public String getSQL(String prompt) {




        Map request = new JSONObject();
        prompt=prompt.replaceAll("#","");
        try {
            request.put("ask", prompt);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestBody body = RequestBody.create(JSONUtil.toJsonStr(request), MediaType.parse("application/json;charset=utf-8"));


        Request requst = new Request.Builder()
                .url(cahtUrl)
                .post(body)
                .build();

        Response response = client.newCall(requst).execute();


        log.info("prompt {} ", prompt);
        String sql = new String(response.body().bytes());
        sql= UnicodeUtil.toString(sql);
        log.info(  "{}  cahtUrl {} ",this.getClass().getSigners(), cahtUrl);
        log.info("sql  {}", sql);

        if (StringUtils.isEmpty(sql)) {
            return "不好意思，我不明白你的意思";
        }
        if(sql.substring(0,1).equals("\"")){
            sql=sql.substring(1,sql.length());

        }
        if( sql.substring(sql.length()-2,sql.length()).equals(";\"")){
            sql=sql.substring(0,sql.length()-2);
            sql=sql +";";

        }

        if( sql.substring(sql.length()-1,sql.length()).equals("\"")){
            sql=sql.substring(0,sql.length()-1);

        }
        sql = sql.replaceAll("\\\\r", "  ");
        sql = sql.replaceAll("\\\\n", "  ");
        sql = sql.replaceAll("\\n", "  ");

        sql = sql.replaceAll("```sql", "");
        sql = sql.replaceAll("```", "");

        return sql;
    }*/
}
